<template>
  <div class="course_manage">
    <Header />

    <div class="container">
      <div class="orientation">
        <div
          class="orientation_item"
          :class="[currentOrientationIndex == index ? 'active_item' : '']"
          v-for="(item, index) in orientationList"
          :key="index"
          @click="orientationClick(item, index)"
        >
          {{ item?.name }}
        </div>
      </div>
      <div class="content">
        <div class="stage" v-for="(item, index) in courseList" :key="index">
          <div class="title">
            <div class="l">
              <span>{{ item.name }}</span>
            </div>
            <div class="r">
              <!-- <el-button type="primary" 
                >添加课程</el-button
              > -->
            </div>
          </div>

          <div class="course">
            <div
              class="course_item"
              v-for="(ite, i) in item?.coursesList"
              :key="i"
              @click="courseClick(ite)"
            >
              <div class="l"><img :src="QNURL + ite.cover" alt="" /></div>
              <div class="r">
                <div class="value">
                  {{ ite.name }}
                </div>
                <div class="user">{{ ite.nickName }}</div>
              </div>
            </div>
            <el-empty
              style="width: 100%; height: 100%"
              description="还没有课程，快去课程发现中添加吧！"
              v-if="!isTeacher && item?.coursesList?.length === 0"
            />
            <div
              class="add_frame"
              v-if="isTeacher"
              @click="addCourseHandel(item)"
            >
              <span>+</span>
            </div>
          </div>
        </div>
        <el-empty
          style="width: 100%; height: 100%"
          description="该方向还没有对应的阶段哦！"
          v-if="courseList?.length === 0"
        />
      </div>
    </div>

    <el-dialog v-model="addCourseVisible" title="添加课程" width="30%">
      <el-form
        ref="ruleFormRef"
        :model="carouseInfo"
        :rules="rules"
        class="demo-ruleForm"
        status-icon
        required
        label-width="80px"
      >
        <el-form-item prop="name" label="课程名称">
          <el-input placeholder="请输入课程名称" v-model="carouseInfo.name" />
        </el-form-item>
        <el-form-item
          prop="courseIntroduce"
          label="课程介绍"
          style="margin-top: 20px"
        >
          <el-input
            placeholder="请输入课程介绍"
            type="textarea"
            v-model="carouseInfo.courseIntroduce"
          />
        </el-form-item>
        <el-form-item label="封面:" prop="cover">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="carouseInfo.cover"
              :src="QNURL + carouseInfo.cover"
              class="img"
            />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="btns">
          <el-button @click="addCourseVisible = false">取消</el-button>
          <el-button type="primary" @click="comfirmHandel">添加</el-button>
        </div>
      </template>
    </el-dialog>
    <RightBottomNav />
  </div>
</template>

<script lang="ts" setup>
import Header from "@/components/header/Header.vue";
import { useRouter } from "vue-router";
import { useCourseManageHooks } from "./hook";
import { QNURL } from "@/config/urlConfig";
import { Plus } from "@element-plus/icons-vue";
import RightBottomNav from "@/components/right-bottom-nav/RightBottomNav.vue";

const {
  orientationList,
  currentOrientationIndex,
  courseList,
  carouseInfo,
  rules,
  addCourseVisible,
  ruleFormRef,
  isTeacher,
  comfirmHandel,
  orientationClick,
  addCourseHandel,
  handleAvatarSuccess,
  beforeAvatarUpload
} = useCourseManageHooks();

const router = useRouter();

function courseClick(item: any) {
  router.push({
    path: "/course",
    query: {
      id: item?.id
    }
  });
}
</script>

<style lang="scss" scoped>
.course_manage {
  width: 100%;
  min-height: 100vh;
  background: var(--bg-color);

  .container {
    width: 1200px;
    margin: 20px auto;

    .orientation {
      width: 100%;
      height: 70px;
      background-color: #fff;
      margin-bottom: 20px;
      display: flex;
      padding: 15px 20px;
      align-items: center;
      line-height: 40px;
      border-radius: 10px;

      &_item {
        // width: 100px;
        height: 100%;
        text-align: center;

        border-radius: 10px;
        cursor: pointer;
        margin-right: 10px;
        padding: 0 10px;
      }

      .active_item {
        background-color: #ccc;
      }
    }

    .content {
      width: 100%;
      min-height: 300px;

      .stage {
        width: 100%;
        margin-top: 20px;

        .title {
          width: 100%;
          height: 50px;
          display: flex;
          align-items: center;

          .l {
            flex: 1;
            font-weight: bold;
            font-size: 20px;
          }

          .r {
            flex: 1;
            display: flex;
            justify-content: flex-end;

            span {
              cursor: pointer;
            }
          }
        }

        .course {
          width: 100%;
          display: flex;
          flex-wrap: wrap;

          &_item {
            width: 230px;
            height: 220px;
            background-color: #fff;
            margin-right: 10px;
            margin-bottom: 10px;
            border-radius: 10px;
            overflow: hidden;
            cursor: pointer;
            transition: all 0.3s;

            &:hover {
              box-shadow: var(--card-shadow);

              img {
                transform: scale(1.1);
              }
            }

            img {
              width: 100%;
              height: 100%;
              transition: all 0.5s;
            }

            .l {
              width: 100%;
              height: 140px;
              overflow: hidden;
            }

            .r {
              flex: 1;
              padding: 10px;

              .value {
                width: 100%;
                height: 20px;
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis;
                // -webkit-line-clamp: 1; /* 控制显示的行数 */
              }

              .user {
                width: 100%;
                color: var(--secondary-color);
                margin-top: 10px;
              }
            }
          }

          .add_frame {
            @extend .course_item;
            background-color: transparent;
            border: 1px dashed #ccc;
            font-size: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
          }
        }
      }
    }
  }
}

img {
  width: 100%;
  height: 100%;
}
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;

  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 200px;
  height: 120px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
  border: 1px dashed var(--el-border-color);
}
</style>
